package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.PingYinUtil;
import com.ruoyi.zsyh.domain.ZsyhPlatformPartsattribute;
import com.ruoyi.zsyh.domain.ZsyhPlatformPartstype;
import com.ruoyi.zsyh.service.IZsyhPlatformPartsattributeService;
import com.ruoyi.zsyh.service.IZsyhPlatformPartstypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhPlatformParts;
import com.ruoyi.zsyh.service.IZsyhPlatformPartsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 配件管理Controller
 * 
 * @author yuexl
 * @date 2021-12-10
 */
@Controller
@RequestMapping("/zsyh/parts")
public class ZsyhPlatformPartsController extends BaseController
{
    private String prefix = "zsyh/parts";

    @Autowired
    private IZsyhPlatformPartsService zsyhPlatformPartsService;
    @Autowired
    private IZsyhPlatformPartsattributeService zsyhPlatformPartsattributeService;
    @Autowired
    private IZsyhPlatformPartstypeService zsyhPlatformPartstypeService;

    @RequiresPermissions("zsyh:parts:view")
    @GetMapping()
    public String parts()
    {
        return prefix + "/parts";
    }

    @RequiresPermissions("zsyh:parts:view")
    @GetMapping("/bannerView")
    public String bannerView()
    {
        return prefix + "/bannerView";
    }


    @RequiresPermissions("zsyh:parts:view")
    @GetMapping("/partsTag")
    public String partsTag()
    {
        return prefix + "/partsTag";
    }

    @RequiresPermissions("zsyh:parts:view")
    @GetMapping("/hotParts")
    public String hotParts()
    {
        return prefix + "/hotParts";
    }

    /**
     * 查询配件管理列表
     */
    @RequiresPermissions("zsyh:parts:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhPlatformParts zsyhPlatformParts)
    {
        zsyhPlatformParts.setOperateType("search");
        ZsyhPlatformPartstype zsyhPartsType = new ZsyhPlatformPartstype();
        List<ZsyhPlatformParts> list;
        if(EmptyUtils.isEmpty(zsyhPlatformParts.getParentId()) && EmptyUtils.isNotEmpty(zsyhPlatformParts.getPartsType())){
            String partsTypes="";
            zsyhPartsType.setpId(zsyhPlatformParts.getPartsType());
            List<ZsyhPlatformPartstype> zsyhPartsTypeList = zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeList(zsyhPartsType);
            for(int i=0;i<zsyhPartsTypeList.size();i++){
                if(i != zsyhPartsTypeList.size()-1){
                    partsTypes += "'"+zsyhPartsTypeList.get(i).getId()+"',";
                }else{
                    partsTypes += "'"+zsyhPartsTypeList.get(i).getId()+"'";
                }
            }
            if(!"".equals(partsTypes)){
                zsyhPlatformParts.setPartsType("("+partsTypes+")");
            }else{
                zsyhPlatformParts.setPartsType("('')");
            }
            startPage();
            list = zsyhPlatformPartsService.selectZsyhPartsListIn(zsyhPlatformParts);
        }else{
            startPage();
            list = zsyhPlatformPartsService.selectZsyhPlatformPartsList(zsyhPlatformParts);
        }
        return getDataTable(list);
    }

    /**
     * 导出配件管理列表
     */
    @RequiresPermissions("zsyh:parts:export")
    @Log(title = "配件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhPlatformParts zsyhPlatformParts)
    {
        zsyhPlatformParts.setOperateType("search");
        List<ZsyhPlatformParts> list = zsyhPlatformPartsService.selectZsyhPlatformPartsList(zsyhPlatformParts);
        ExcelUtil<ZsyhPlatformParts> util = new ExcelUtil<ZsyhPlatformParts>(ZsyhPlatformParts.class);
        return util.exportExcel(list, "配件管理数据");
    }

    /**
     * 新增配件管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存配件管理
     */
    @RequiresPermissions("zsyh:parts:add")
    @Log(title = "配件管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhPlatformParts zsyhPlatformParts)
    {
        StringBuffer sb = new StringBuffer();
        //编码规则，产品名字缩写+"_"+该产品在此分类下面的顺序
        String tempStr = PingYinUtil.getFirstSpell(zsyhPlatformParts.getPartsName());
        sb.append(tempStr);
        sb.append("_");
        //查询该分类下面所有的产品
        ZsyhPlatformParts zsyhPlatformParts1 = new ZsyhPlatformParts();
        zsyhPlatformParts1.setPartsType(zsyhPlatformParts.getPartsType());
        List<ZsyhPlatformParts> zsyhPartsList = zsyhPlatformPartsService.selectZsyhPlatformPartsList(zsyhPlatformParts1);
        if(zsyhPartsList.size()>0){
            sb.append("00"+(zsyhPartsList.size()+1));
        }else{
            sb.append("001");
        }
        zsyhPlatformParts.setPartsNumber(sb.toString());
        //判断名字是否重复
        zsyhPlatformParts1.setPartsName(zsyhPlatformParts.getPartsName());
        zsyhPlatformParts1.setOperateType("edit");
        if(zsyhPlatformPartsService.selectZsyhPlatformPartsList(zsyhPlatformParts1).size()>0){
            return  error("该分类下面已经存在同样的产品名称！");
        }
        //添加排序，默认在当前最大的排序字段下一个数字
        zsyhPartsList = zsyhPlatformPartsService.selectZsyhPlatformPartsList(new ZsyhPlatformParts());
        zsyhPlatformParts.setPartsSort(zsyhPartsList.get(0).getPartsSort()+1);
        return toAjax(zsyhPlatformPartsService.insertZsyhPlatformParts(zsyhPlatformParts));
    }

    /**
     * 修改配件管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPlatformParts zsyhPlatformParts = zsyhPlatformPartsService.selectZsyhPlatformPartsById(id);
        mmap.put("zsyhPlatformParts", zsyhPlatformParts);
        return prefix + "/edit";
    }

    /**
     * 修改保存配件管理
     */
    @RequiresPermissions("zsyh:parts:edit")
    @Log(title = "配件管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhPlatformParts zsyhPlatformParts)
    {
        StringBuffer sb = new StringBuffer();
        //编码规则，产品名字缩写+"_"+该产品在此分类下面的顺序
        String tempStr = PingYinUtil.getFirstSpell(zsyhPlatformParts.getPartsName());
        sb.append(tempStr);
        sb.append("_");
        //查询该分类下面所有的产品
        ZsyhPlatformParts zsyhPlatformParts1 = new ZsyhPlatformParts();
        zsyhPlatformParts1.setPartsType(zsyhPlatformParts.getPartsType());
        List<ZsyhPlatformParts> zsyhPartsList = zsyhPlatformPartsService.selectZsyhPlatformPartsList(zsyhPlatformParts1);
        if(zsyhPartsList.size()>0){
            sb.append("00"+(zsyhPartsList.size()+1));
        }else{
            sb.append("001");
        }
        //判断名字是否重复
        zsyhPlatformParts1.setPartsName(zsyhPlatformParts.getPartsName());
        zsyhPlatformParts1.setOperateType("edit");
        zsyhPartsList = zsyhPlatformPartsService.selectZsyhPlatformPartsList(zsyhPlatformParts1);
        if(zsyhPartsList.size()>1){
            if(!zsyhPlatformParts.getId().equals(zsyhPartsList.get(0).getId())){
                return  error("该分类下面已经存在同样的产品名称！");
            }
        }
        zsyhPlatformParts.setPartsNumber(sb.toString());
        return toAjax(zsyhPlatformPartsService.updateZsyhPlatformParts(zsyhPlatformParts));
    }

    /**
     * 删除配件管理
     */
    @RequiresPermissions("zsyh:parts:remove")
    @Log(title = "配件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //如果有型号的，则不能删除
        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute = new ZsyhPlatformPartsattribute();
        zsyhPlatformPartsattribute.setPartsId(ids);
        if(zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeList(zsyhPlatformPartsattribute).size()>0){
            return error("该产品存在相关规格，不能删除，请先删除规格信息");
        }
        return toAjax(zsyhPlatformPartsService.deleteZsyhPlatformPartsByIds(ids));
    }

    /**
     * 查询产品规格列表
     */
    @RequiresPermissions("zsyh:parts:list")
    @PostMapping("/listChild")
    @ResponseBody
    public TableDataInfo listChild(String mid)
    {
        startPage();
        ZsyhPlatformPartsattribute zsyhPartsAttribute = new ZsyhPlatformPartsattribute();
        zsyhPartsAttribute.setPartsId(mid);
        List<ZsyhPlatformPartsattribute> list = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeList(zsyhPartsAttribute);
        return getDataTable(list);
    }

    /**
     * 置顶配件
     */
    @RequiresPermissions("zsyh:parts:remove")
    @Log(title = "配件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/moveTop")
    @ResponseBody
    public AjaxResult moveTop(String id)
    {
        List<ZsyhPlatformParts> zsyhPlatformPartsList = zsyhPlatformPartsService.selectZsyhPlatformPartsList(new ZsyhPlatformParts());
        Long partsSort = zsyhPlatformPartsList.get(0).getPartsSort();
        ZsyhPlatformParts zsyhPlatformParts = zsyhPlatformPartsService.selectZsyhPlatformPartsById(id);
        zsyhPlatformParts.setPartsSort(partsSort+1);
        return toAjax(zsyhPlatformPartsService.updateZsyhPlatformParts(zsyhPlatformParts));
    }

    /**
     * 上移,下移配件
     */
    @Transactional
    @RequiresPermissions("zsyh:parts:edit")
    @Log(title = "配件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/moveUp")
    @ResponseBody
    public AjaxResult moveUp(String id,String otherId,Long partsSort,Long otherPartsSort)
    {
        ZsyhPlatformParts zsyhPlatformParts = new ZsyhPlatformParts();
        ZsyhPlatformParts otherZsyhPlatformParts = new ZsyhPlatformParts();
        zsyhPlatformParts.setId(id);
        zsyhPlatformParts.setPartsSort(otherPartsSort);
        otherZsyhPlatformParts.setId(otherId);
        otherZsyhPlatformParts.setPartsSort(partsSort);
        int num = zsyhPlatformPartsService.updateZsyhPlatformParts(zsyhPlatformParts);
        if(num >0){
            zsyhPlatformPartsService.updateZsyhPlatformParts(otherZsyhPlatformParts);
        }
        return toAjax(num);
    }
}
